/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcloudclient.fileSystem;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Tomas
 */
public class FileNodeBuilder {

    private DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss dd.MM.YYYY");

    public FileNodeBuilder() {
    }

    public FileNode build(String path) {
        File root = new File(path);
        return createNewSettings(root);
    }

    private FileNode createNewSettings(File file) {
        File[] listFiles = file.listFiles();
        List<FileNode> childs = new ArrayList<FileNode>();
        for (File fileNext : listFiles) {
            if (fileNext.isDirectory()) {
                childs.add(createNewSettings(fileNext));
            } else {
                childs.add(new FileNode(fileNext, FileStatusEnum.NEW, new Date(fileNext.lastModified()), new ArrayList<FileNode>()));
            }
        }
        return new FileNode(file, FileStatusEnum.NEW, new Date(file.lastModified()), childs);
    }

    public FileNode updateFilesSettings(File file, FileNode rootNode) {
        return updateFileSettings(file, rootNode);
    }

    private FileNode updateFileSettings(File file, FileNode rootNode) {
        List<FileNode> pomList = new ArrayList<FileNode>(rootNode.getChilds());
        if (file.isDirectory()) {
            for (File childfile : file.listFiles()) {
                FileNode findNode = findFileNode(childfile.getName(), pomList);
                if (findNode == null) {
                    if (childfile.isDirectory()) {
                        rootNode.getChilds().add(createNewSettings(childfile));
                    } else {
                        rootNode.getChilds().add(new FileNode(childfile, FileStatusEnum.NEW, new Date(childfile.lastModified()), new ArrayList<FileNode>()));
                    }
                } else {
                    findNode = updateFileSettings(childfile, findNode);
                    updateFileNode(findNode, childfile);
                    pomList.remove(findNode);
                }
            }
            if (!pomList.isEmpty()) {
                for (FileNode fileNode : pomList) {
                    if (fileNode.getStatus() == FileStatusEnum.NEW) {
                        rootNode.getChilds().remove(fileNode);
                    } else {
                        fileNode.setStatus(FileStatusEnum.DELETE);
                    }
                }
            }
        } else {
            updateFileNode(rootNode, file);
        }
        return rootNode;
    }

    private FileNode updateFileNode(FileNode findNode, File childfile) {
        if (childfile.lastModified() > findNode.getLastChange()) {
            if (findNode.getStatus() == FileStatusEnum.NOMODIFY) {
                findNode.setStatus(FileStatusEnum.MODIFY);
                findNode.setLastChange(childfile.lastModified());
            }
        }

        return findNode;
    }

    private FileNode findFileNode(String name, List<FileNode> list) {
        for (FileNode fileNode : list) {
            if (fileNode.getName().equals(name)) {
                return fileNode;
            }
        }
        return null;
    }
}
